﻿using Prism.Mvvm;
using System.Collections.Generic;
using System.Windows;
using TomatoClock.Utils;

namespace TomatoClock.ViewModels
{
    public class MainViewModel : BindableBase
    {
        public string Title { get; set; }
        public string MessageTip { get; set; }

        public List<string> IntervalList { get; set; }
        public int RestOfTime { get; set; }
        public int TotalTime { get; set; }


        private string restTime= "倒计时：00:00:00";

        public string RestTime
        {
            get { return restTime; }
            set
            {
                restTime = $"倒计时：{value}";
                RaisePropertyChanged();
            }
        }

        private string selectedInterval;

        public string SelectedInterval
        {
            get { return selectedInterval; }
            set
            {
                var b = SetProperty(ref selectedInterval, value);
                if (b)
                {
                    timer.Stop();

                    var timeInterval = DeleteChineseCharacters(selectedInterval);

                    RestOfTime = int.Parse(timeInterval) * 60 * 1000;
                    TotalTime = RestOfTime;
                    TimerPercent = 0;

                    RestTime =  TimerConverter.Instance.timeToString(RestOfTime);
                  
                    timer.Start();
                }
            }
        }


        private double timerPercent;

        public double TimerPercent
        {
            get { return timerPercent; }
            set { timerPercent = value; RaisePropertyChanged(); }
        }

        private System.Timers.Timer timer;

        public MainViewModel()
        {
            Title = System.Configuration.ConfigurationManager.AppSettings["Title"];
            MessageTip = System.Configuration.ConfigurationManager.AppSettings["MessageTip"];

            IntervalList = new List<string>()
            {
                "15分钟",
                "30分钟",
                "45分钟",
                "60分钟",
                "90分钟"
            };

            timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.Elapsed += Timer_Elapsed;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            RestOfTime-=1000;

            if (RestOfTime <= 0)
            {
                timer.Stop();
                MessageBox.Show(MessageTip);
                RestOfTime = TotalTime;
                timer.Start();
            }

            RestTime = TimerConverter.Instance.timeToString(RestOfTime);

            TimerPercent = System.Math.Round((double)(TotalTime - RestOfTime) / TotalTime, 3);
        }

        /// <summary>
        /// 删除字符串中的中文
        /// </summary>
        public string DeleteChineseCharacters(string str)
        {
            string retValue = str;
            if (System.Text.RegularExpressions.Regex.IsMatch(str, @"[\u4e00-\u9fa5]"))
            {
                retValue = string.Empty;
                var strsStrings = str.ToCharArray();
                for (int index = 0; index < strsStrings.Length; index++)
                {
                    if (strsStrings[index] >= 0x4e00 && strsStrings[index] <= 0x9fa5)
                    {
                        continue;
                    }
                    retValue += strsStrings[index];
                }
            }
            return retValue;
        }
    }
}
